package Example9;

import Example1.Parent;

import java.util.Collections;
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int D = scanner.nextInt();
        int max = 0;
        //    读取N*N数组
        int sum = 0;

        int []arr=new int[N*N];
        int []sumArr=new int[N*N];
        int []line1=new int[N-D+1];
        int []line2=new int[N-D+1];
        int k=0;
        for (int i=0;i<N*N;i++){
            arr[i]=scanner.nextInt();
            sum+=arr[i];
            if ((i+1)%D==0){
                sumArr[k++]=sum;
                sum=0;
            }
        }

        int start=0,interval=N+1,count=D;
        sum=0;
        for (int i=0;i<line1.length;i++){
            while (count>0) {
                sum+=arr[start];
                start+=interval;
                count--;
            }
            line1[i]=sum;
            count=D;
        }

        start=N-1;
        interval=N-1;
        sum=0;
        count=D;
        for (int i = 0; i < line2.length; i++) {
            while (count>0) {
                sum+=arr[start];
                start+=interval;
                count--;
            }
            line2[i]=sum;
            count=D;
        }

        for (int i = 0; i < sumArr.length; i++) {
            if (sumArr[i]>max)
                max=sumArr[i];
        }
        for (int i=0;i<line1.length;i++){
            if(line1[i]>max)
                max=line1[i];
        }
        for (int i=0;i<line2.length;i++){
            if(line2[i]>max)
                max=line2[i];
        }

        System.out.println(max);
    }
}
